import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Users from '../components/childpage/Users.vue'
import Roles from '../components/childpage/Roles.vue'
import Rights from '../components/childpage/Rights.vue'
import Reports from '../components/childpage/Reports.vue'
import Params from '../components/childpage/Params.vue'
import Orders from '../components/childpage/Orders.vue'
import Goods from '../components/childpage/Goods.vue'
import Categories from '../components/childpage/Categories.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    redirect: '/users',
    children:[
      {path: '/users', name: '', component:Users},
      {path: '/roles', name: '', component:Roles},
      {path: '/rights', name: '', component:Rights},
      {path: '/reports', name: '', component:Reports},
      {path: '/params', name: '', component:Params},
      {path: '/orders', name: '', component:Orders},
      {path: '/goods', name: '', component:Goods},
      {path: '/categories', name: '', component:Categories},
    ]
  },
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to, from, next) => {
  //to and from are Route Object,next() must be called to resolve the hook}
  if(to.path === '/login')
    return next();
  
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');

  if(!tokenStr)
    return next('/login');

  next();
})

export default router
